#include "MainHeader.h"
cMapWindow::~cMapWindow(){}
cMapWindow::cMapWindow(){ }
int cMapWindow::Init(HWND setup){
hwndBound=setup;
RECT rMap;
	// Fill in the window class structure with parameters 
	// that describe the main window. 
	
	wcMap.cbSize = sizeof(wcMap);		   // size of structure 
	wcMap.style = CS_HREDRAW | 
		CS_VREDRAW; 				   // redraw if size changes 
	wcMap.lpfnWndProc = MapProc;	  // points to window procedure 
	wcMap.cbClsExtra = NULL;				// no extra class memory 
	wcMap.cbWndExtra =NULL; 			   // no extra window memory 
	wcMap.hInstance = hGlobal;		   // handle to instance 
	wcMap.hIcon = NULL; 			 // predefined app. icon 
	wcMap.hCursor = NULL;				// predefined arrow 
	// wcMap.hbrBackground = GetStockObject( WHITE_BRUSH);				   // white background brush 
	wcMap.lpszMenuName =  NULL;    // name of menu resource 
	wcMap.lpszClassName = "MapClass";  // name of window class 
	int Shit=GetLastError();
	
	// Register the window class. 
	
	RegisterClassEx(&wcMap); 
	//Create the Window
	 Shit=GetLastError();
	hwndMap = CreateWindow("MapClass",NULL, WS_CHILD| WS_VISIBLE,0,0,0,0,hwndMain,0,hGlobal,0);
		hScroll.maxscroll = vScroll.maxscroll = 100;
	if(Shit=vScroll.MakeMe(SBS_VERT,hwndMain, ID_MAPVSCROLL)) 
		MessageBox(0,"We have a problem SBS_VERT", "None", MB_OK);

	if(hScroll.MakeMe(SBS_HORZ,hwndMain, ID_MAPHSCROLL))
		MessageBox(0,"We have a problem SBS_HORZ", "None", MB_OK);
	
	hScroll.Change();
	vScroll.Change();
    
    imgMap.Create(256, 256);
	bbMap.Create(256, 256);
	ShowWindow(hwndMap,SW_SHOW);	
return 0;
}
int cMapWindow::PlaceTile(int X, int Y){
int tX=X/16, tY=Y/16;
    //Only place on the level array for now

if(WorkArea.chkForeground.value()==1){
clsGame.ThisForeground.TileBuf2D[(tY+vScroll.curscroll)*clsGame.ThisLevelData.X + (tX+hScroll.curscroll)]=Tools.Tile;
clsGame.ThisForeground.Dirty = 1;
}
if(WorkArea.chkMidLayer.value()==1){
clsGame.ThisLevelData.TileBuf2D[(tY+vScroll.curscroll)*clsGame.ThisLevelData.X + (tX+hScroll.curscroll)]=Tools.Tile;
clsGame.ThisLevelData.Dirty = 1;
}
if(WorkArea.chkBlackLayer.value()==1){
clsGame.ThisBackLayer.TileBuf2D[(tY+vScroll.curscroll)*clsGame.ThisLevelData.X + (tX+hScroll.curscroll)]=Tools.Tile;
clsGame.ThisBackLayer.Dirty = 1;
}
if(WorkArea.chkClipdata.value()==1){
clsGame.ThisClipData.TileBuf2D[(tY+vScroll.curscroll)*clsGame.ThisLevelData.X + (tX+hScroll.curscroll)]=WorkArea.cboClipdata.GetListIndex();
}

return 0; 
};
int cMapWindow::Resize(){
  RECT Bound;
  RECT Map;
  RECT Main;
  int newmax=0;
  GetWindowRect(hwndMain, &Main);
  GetWindowRect(hwndBound, &Bound);
  Map.left=2;
  Map.top=2;
  Map.right = Main.right - 384;
  Map.bottom = Main.bottom - 64; 
  MoveWindow(hwndMap,Map.left, Map.top, Map.right, Map.bottom,0);

  newmax=clsGame.ThisLevelData.X;
  newmax=((Map.right-Map.left)/16 < newmax ? newmax -(Map.right-Map.left)/16  : 0);
 
  hScroll.maxscroll = newmax;
  SetScrollRange( hScroll.me, SB_CTL, 0, hScroll.maxscroll, FALSE);
  hScroll.resize(Map.left+3,Map.bottom-38,Map.right - Map.left + 1, 16);
 
  newmax=clsGame.ThisLevelData.Y;
  newmax=((Map.bottom-Map.top)/16 < newmax ? newmax -(Map.bottom-Map.top)/16  : 0);
 
  vScroll.maxscroll = newmax;
  SetScrollRange( vScroll.me, SB_CTL, 0, vScroll.maxscroll, FALSE);

 
  vScroll.resize(Map.right+2,Map.top+5,16,Map.bottom-Map.top-32);
   

  MoveWindow(hwndBound, 2, 0, Map.right+18,  Map.bottom-2,0);
  return 0;

}
LRESULT CALLBACK MapProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	PAINTSTRUCT ps;
	HDC hdc;
    RECT rMap;
	char strClip[1024]={0};
	int ClipNo=0;
	switch (message) 
	{

		 
	
	case WM_PAINT:
		hdc = BeginPaint(hWnd, &ps);
		GetWindowRect(hWnd,&rMap);
        BitBlt(hdc, 8, 11, rMap.right - rMap.left, rMap.bottom - rMap.top-64, bbMap.DC(), MapWindow.hScroll.curscroll*16,  MapWindow.vScroll.curscroll*16, SRCCOPY);
		EndPaint(hWnd, &ps);
		DeleteDC(hdc);
		break;
	case WM_VSCROLL:	// exact same idea, but V scroll instead of H scroll
		
	    break;
	case WM_HSCROLL:
		
		break;
	case WM_LBUTTONDOWN:
        MapWindow.PlaceTile(GetX(lParam),GetY(lParam));
		//Redraw
		
		clsGame.DrawAll=1;
        clsGame.DrawRoom();
	    
            return 0;
	
		break;
	case WM_RBUTTONDOWN:
		break;case WM_RBUTTONUP:
		
	
		break;
	case WM_LBUTTONUP:
		
		break;
	case WM_KEYDOWN:						

	

		break;
	case WM_MOUSEMOVE:
		ClipNo= clsGame.ThisClipData.TileBuf2D[((GetY(lParam)/16)+MapWindow.vScroll.curscroll)*clsGame.ThisClipData.X + ((GetX(lParam)/16)+MapWindow.hScroll.curscroll)];
		sprintf(strClip,"Current Clip: %X-%s",ClipNo
       
		,   clsGame.strMovement[ClipNo].c_str());
		SetWindowText(GetDlgItem(hwndMain,ID_clipshow),strClip);

		break;
	case WM_SIZE:
		
		
		break;
	case WM_DESTROY:
		
		break;
		

		
	}
	return DefWindowProc(hWnd, message, wParam, lParam);
}
